﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using NOSDC.Models;
using NOSDC.Pocos;
using NOSDC.Services;

namespace NOSDC.Controllers
{
    public class CommentsController : Controller
    {
        private readonly ICommentService _service;

        public CommentsController()
        {
            _service = new CommentService();
        }

        public ActionResult GetComments(int articleid, int? page)
        {
            if (page == null)
                page = 1;

            var comments = new CommentViewModel(articleid)
                               {
                                   Comments = _service.GetCommentsByArticle(articleid, (int)page, Utility.CommentPager.MaxRecordsPerPage),
                                   PostedComment = new Comment { ArticleID = articleid },
                                   TotalComments = _service.TotalComments(articleid),
                                   CurrentPage = (int)page
                               };

            return PartialView("Comment/Comments", comments);
        }

        public ActionResult GetMoreComments(int articleid, int? page)
        {
            var moreComments = new MoreCommentsViewModel();

            if (Request.IsAjaxRequest())
            {
                if (page != null && articleid > 0)
                {
                    page++;
                    var comments = _service.GetCommentsByArticle(articleid, (int)page, Utility.CommentPager.MaxRecordsPerPage);
                    var cm = comments.Select(c => 
                                                {
                                                     var membershipUser = Membership.GetUser(c.UserID);
                                                     return membershipUser != null ? new MoreCommentsModel
                                                                                          {
                                                                                              Comment = c.Message, 
                                                                                              PostedByID = c.UserID, 
                                                                                              PostedBy = membershipUser.UserName, 
                                                                                              PostDate = c.DatePosted.ToString("MMMM d, yyyy, hh:mm:ss"),
                                                                                          } : null;
                                                 }).ToList();

                    moreComments.TotalComments = _service.TotalComments(articleid);
                    moreComments.Comments = cm;
                }
            }

            return Json(moreComments, JsonRequestBehavior.AllowGet);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult PostComment(CommentViewModel comments)
        {
            if (ModelState.IsValid)
            {
                var user = Membership.GetUser();
                if (user != null)
                {
                    comments.PostedComment.UserID = new Guid(user.ProviderUserKey.ToString());
                    _service.PostComment(comments.PostedComment);
                }
            }
            return RedirectToAction("GetComments", new { articleid = comments.PostedComment.ArticleID });
        }
    }
}